<div class="section" id="purchase" style="font-size:0.9em">
<form name="formlist" id="formlist" action="<?php echo $action?>" method="post" enctype="multipart/form-data"  onSubmit="return false">
	<div id="purchase_panel">
		<div class="section-title">
			<?php echo $this->document->text['text_header_returnproducttosupplier']?>  
		</div>
		
		<div class="section-content padding1">
			<div>
				<p>
					<b><?php echo $this->document->text['text_ordercode']?></b> : <input type="text" name="ordernumber" value="<?php echo $order['ordernumber']?>" class="text" size="10" /><input type="hidden" name="ordertype" value="<?php echo $order['ordertype']?>" />
					<b><?php echo $this->document->text['text_orderdate']?></b> : 
						<input type="text" id="orderdate" name="orderdate" value="<?php echo $this->date->formatMySQLDate($order['orderdate'])?>" class="text" size="10"/>
						<?php echo $error['orderdate']?>
					<b><?php echo $this->document->text['text_cashier']?></b> : <select name="employeeid" style="width:200px"><?php echo $cb_users?></select>
					<b><?php echo $this->document->text['text_paymenttype']?></b> : <select name="paymenttype" style="width:100px"><?php echo $cb_paymenttypes?></select>
				 </p>
				 <p>
					<div class="left">
						<b><?php echo $this->document->text['text_supplier']?></b> : <select id="cbpartner" name="partnerid" style="width:300px"><?php echo $cb_suppliers?></select>
						<a id="quickaddsupplier" class="button"><?php echo $this->document->text['button_quickadd']?></a>
					</div>
					<div class="right">
						<b>&nbsp;&nbsp;&nbsp;<?php echo $this->document->text['text_barcode']?> : </b><input type="text" class="text" id="barcodereader" name="barcodereader" size="10" />&nbsp;<input type="button" class="button" value="<?php echo $this->document->text['button_ok']?>" onclick="getbarcode()" /> ||| <a class="button" id="searchproduct"><?php echo $this->document->text['button_searchproduct']?></a>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<a class="button" onclick="deleteitem()"><?php echo $this->document->text['button_deleteitem']?></a>
					</div>
					<div class="clearer"></div>
				 </p>
			 </div>
			 
			 <div style=" height:270px; overflow:auto; font-size:11pt; background:#999999; border:1px solid #CCCCCC">
				<table class="data-table" style="width:1250px">
					<thead style="font-size:9pt">
						<th width="300px"><?php echo $this->document->text['text_productname']?></th>
						<th width="130px"><?php echo $this->document->text['text_stock']?></th>
						<th width="50px"><?php echo $this->document->text['text_unit']?></th>
						<th width="50px"><?php echo $this->document->text['text_qty']?></th>
						<th width="100px"><?php echo $this->document->text['text_price']?></th>
						<th width="100px"><?php echo $this->document->text['text_subtotal']?></th>
						<th width="40px">Tax (%)</th>
						<th width="80px"><?php echo $this->document->text['text_taxamount']?></th>
						<th width="120px"><?php echo $this->document->text['text_grandtotal']?> (VAT)</th>
						<th width="50px">%<?php echo $this->document->text['text_discount']?>.</th>
						<th width="120px"><?php echo $this->document->text['text_discount']?></th>
						<th width="110px"><?php echo $this->document->text['text_note']?></th>
					</thead>
					<tbody id="orderdetail_panel">
						<?php foreach($orderdetails as $ord) {?>
						<tr>
							<td>
								<input class="inputchk" type="checkbox" name="item[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['orderdetailid']?>" />
								<?php echo $ord['productname']?>
								<input type="hidden"  name="orderdetailid[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['orderdetailid']?>"  />
								<input type="hidden"  name="productid[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['productid']?>"  />
							</td>
							<td><select name="stockid[<?php echo $ord['orderdetailid']?>]" style="width:120px"><?php echo $ord['cb_stocks']?></select></td>
							<td><?php echo $ord['unit']?></td>
							<td><input class="text calc qty" type="text"  name="qty[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['qty']?>" style="width:40px" /></td>
							<td><input class="text calc orderprice" type="text" name="orderprice[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['orderprice']?>" style="width:80px" /></td>
							<td><span class="subtotal"><?php echo $ord['subtotal']?></span></td>
							<td><input class="text calc tax" type="text" name="tax[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['tax']?>" style="width:20px" /></td>
							<td><span class="taxamount"><?php echo $ord['taxamount']?></span></td>
							<td><span class="grandtotal"><?php echo $ord['grandtotal']?></span></td>
							<td><input class="text calc discount" type="text" name="discount[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['discount']?>" style="width:20px" />%</td>
							<td><span class="discountamount"><?php echo $ord['discountamount']?></span></td>
							<td><input class="text" type="text" name="note[<?php echo $ord['orderdetailid']?>]" value="<?php echo $ord['note']?>" style="width:90px" /></td>
						</tr>	
						<?php } ?>
						
					
					</tbody>
				</table>
			 </div>
			 
		 
			 
			<div class="left">
				<span style="margin-right:10px"><b><?php echo $this->document->text['text_totalcost']?> : </b> <input type="text" name="order_subtotal" class="text total_subtotal disabled number" readonly="readonly" value="<?php echo $order['subtotal']?>" size="5" /> </span>
				<span style="margin-right:10px"><b><?php echo $this->document->text['text_totaltax']?> : </b> <input type="text" name="order_taxamount" class="total_taxamount text disabled number" readonly value="<?php echo $order['taxamount']?>" size="5" /></span>
				<span style="margin-right:10px"><b><?php echo $this->document->text['text_totaldiscount']?> : </b> <input type="text" name="order_discountamount" class="total_discountamount text disabled number" readonly value="<?php echo $order['discountamount']?>" size="5" /><br />
				<span style="margin-right:10px; font-size:12pt; font-weight:bold; color:red "><?php echo $this->document->text['text_grandtotal']?> : <input type="text" name="order_grandtotal" class="total_grandtotal text disabled number" readonly value="<?php echo $order['grandtotal']?>" size="20" /></span>
			 </div>
			 <div class="right" style="width:40%">
				<span style="margin-right:10px; "><b><?php echo $this->document->text['text_totalpaid']?> : </b> <input class="text paymentamount" type="text" name="order_paymentamount" value="<?php echo $order['paymentamount']?>" style="width:90px; font-size:12pt; text-align:right; background-color:red; color:#FFFFFF" /></span>
				<span style="margin-right:10px; color:red "><b><?php echo $this->document->text['text_debtbalance']?> : </b><input type="text" readonly name="order_deptamount" class="total_deptamount" value="<?php echo $order['deptamount']?>" size="6" /><br />
				<a class="button payall" href="#"><?php echo $this->document->text['button_payall']?></a>
			 </div>
			 <div class="clearer">&nbsp;</div>
			 
		</div>
		
		<div class="section-title">   
			<?php if($allowdelete) {?>
			<div class="button left">
				<input type="button" name="delete" value="<?php echo $this->document->text['button_delete']?>" class="button" onclick="deleteorder()"/>
			</div> 
			<?php } ?>
			<div class="button right">
				<input type="button" name="save" value="<?php echo $this->document->text['button_save']?>" class="button" onclick="fsubmit(0)"/>
				<input type="button" name="saveprint" value="<?php echo $this->document->text['button_saveprint']?>" class="button" onclick="fsubmit(1)"/>
				<input type="button" value="<?php echo $this->document->text['button_close']?>" class="button" onclick="window.close()"/>   
				<input type="hidden" name="orderid" value="<?php echo $order['orderid']?>" />
				<input type="hidden" class="print" name="print" value="" />   
			</div>
			<div class="clearer">^&nbsp; </div>
		</div>
	</div>
	
	<div id="supplier_panel" style="display:none">
		<?php include(DIR_TEMPLATE."category/supplier_subform.tpl")?>
	</div>
	
	<div id="searchproduct_panel" style="display:none">
		<?php include(DIR_TEMPLATE."category/searchproduct_subform.tpl")?>
	</div>
	
	
</form>    
</div>

<script language="javascript">
 	$(function() {
		$("#orderdate").datepicker({
				changeMonth: true,
				changeYear: true,
				dateFormat: 'dd/mm/yy'
				});
		});
		
	/*$("#quickaddsupplier").click(function(){
		$("#purchase_panel").hide();
		$("#supplier_panel").show();
	});*/
	
	/*$("#quickaddsupplier_cancel").click(function(){
		$("#quick_groupname").val("");
		$(".error_quick_groupname").html("");
		$("#product_panel").show();
		$("#group_panel").hide();
	});*/
	
	$(document).ready(function(){
		
		$("#searchproduct").click(function(){
			savetemp();
			var result = window.showModalDialog("index.php?route=category/product/search&orderid=<?php echo $orderid?>", "", "dialogWidth:1000px; dialogHeight:600px; center:yes");
			window.location.reload();
		});
		
		$("#quickaddsupplier").click(function(){
			var result = window.showModalDialog("index.php?route=category/supplier/quickadd", "", "dialogWidth:1000px; dialogHeight:600px; center:yes");
			//window.location.reload();
		});
		
				
		$(".calc").keyup(function(e){
			
			
			var tr = $(this).parent().parent();
			
			calcgrid(tr);
			
			
			num = formateNumber($(this).val().replace(/,/g,""));
			$(this).val(num);
			
			sumOrder();
		});
		
		
		$(".calc").keypress(function (e)
		{	
		  return isNumber(e.which);
		});
		
		$(".paymentamount").keypress(function (e)
		{	
		  return isNumber(e.which);
		});
		
		$(".paymentamount").keyup(function (e)
		{	
		  sumOrder();
		});
		
		$("#barcodereader").keyup(function (e)
		{	
		  if(e.keyCode == 13)
		  {
		  	getbarcode();
		  }
		});
		
		$(".payall").click(function(){
			$(".paymentamount").val($(".total_grandtotal").val());
			sumOrder();
		});
		
		sumOrder();
		
		<?php if($_GET['print'] == '1' ) {?>
			var result = window.showModalDialog("index.php?route=voucher/report/returnsupplier&orderid=<?php echo $orderid?>", "", "dialogWidth:750px; dialogHeight:600px; center:yes");
		<?php } ?>
		
		
	});
	
	function getbarcode()
	{
		var barcodeid = $("#barcodereader").val(); 
			
		var url = "?route=voucher/returnsupplier/barcode&orderid=<?php echo $orderid?>&barcode="+barcodeid;
		
		$.post(url, 
				$("#formlist").serialize(), 
				function(data)
				{
					if(data!="")
					{
						alert(data);
						//window.close();
					}
					else
					{
						savetemp();
						window.location.reload();
					}
				}
		);
	}
	
	function sumOrder()
	{
		var subtotal = 0;
		$(".subtotal").each(function(){
			subtotal += CDbl($(this).html());
		});
		
		var taxamount = 0;
		$(".taxamount").each(function(){
			taxamount += CDbl($(this).html());
		});
		
		var discountamount = 0;
		$(".discountamount").each(function(){
			discountamount += CDbl($(this).html());
			var tr = $(this).parent().parent();
			calcgrid(tr);
		});
		
		$(".total_subtotal").val(formateNumber(subtotal));
		$(".total_taxamount").val(formateNumber(taxamount));
		$(".total_discountamount").val(formateNumber(discountamount));
		
		var grandtotal = subtotal + taxamount - discountamount;
		$(".total_grandtotal").val(formateNumber(grandtotal));
		
		var paymentamount = CDbl($(".paymentamount").val());
		var deptamount = grandtotal - paymentamount;
		
		$(".paymentamount").val(formateNumber(paymentamount));
		$(".total_deptamount").val(formateNumber(deptamount));
	}
	
	function calcgrid(tr){
					
		qty =  tr.find(".qty").val().replace(",","");
		orderprice = tr.find(".orderprice").val().replace(",","");
		
		
		subtotal = qty * orderprice;
		tr.find(".subtotal").html(formateNumber(subtotal));
		
		tax =  tr.find(".tax").val().replace(",","");
		
		taxamount = roundNumber(subtotal / 100 * tax,2);
		tr.find(".taxamount").html(formateNumber(taxamount));
		
		grandtotal =  roundNumber(subtotal + taxamount,2);
		tr.find(".grandtotal").html(formateNumber(grandtotal));
		
		discount =  tr.find(".discount").val().replace(",","");
		discountamount = roundNumber(grandtotal / 100 * discount,2);
		
		tr.find(".discountamount").html(formateNumber(discountamount));
	}
	
	function CDbl(number)
	{
		number = number.replace(",","");
		if((parseFloat(number)+"") == "NaN"){
			return 0;
		}
		else
		{
			return parseFloat(number);
		}
	}
	
	
	function savetemp()
	{
		$.post("?route=voucher/returnsupplier/saveordertemp&orderid=<?php echo $orderid?>", 
				$("#formlist").serialize(), 
				function(data)
				{
					if(data!="")
					{
						//alert(data);
						//window.close();
					}
				}
		);
	}
	
	function deleteitem()
	{
		$.post("?route=voucher/returnsupplier/delete&orderid=<?php echo $orderid?>", 
				$("#formlist").serialize(), 
				function(data)
				{
					if(data!="")
					{
						alert(data);
						window.location.reload();
						//window.close();
					}
				}
		);
	}
	
	function deleteorder()
	{
		$.post("?route=voucher/search/deleteorder&orderid=<?php echo $orderid?>", 
				$("#formlist").serialize(), 
				function(data)
				{
					alert("Delete successfully!");
					window.close();
					
				}
		);
	}
	
	function fsubmit(p){
		if(p == 1)
		{
			$(".print").val("1");
		}
		else{
			$(".print").val("0");
		}
		$("#formlist").submit();
	}
</script>
